//
//  LoginViewController.m
//  bus
//
//  Created by hychen on 2017/4/5.
//  Copyright © 2017年 Hychen. All rights reserved.
//

#import "LoginViewController.h"
#import "ForgetPasswordViewController.h"
#import "UINavigationController+FDFullscreenPopGesture.h"
#import "LoginViewModel.h"

@interface LoginViewController () <UITextFieldDelegate>

@property (nonatomic, assign) LoginComeFromType comeFrom;

@property (weak, nonatomic) IBOutlet UILabel *appLabel;
@property (weak, nonatomic) IBOutlet UIImageView *avatorImage;
@property (weak, nonatomic) IBOutlet UIView *accountView;
@property (weak, nonatomic) IBOutlet UITextField *accountTextField;
@property (weak, nonatomic) IBOutlet UIView *passwordView;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UIButton *forgetButton;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet UIView *rememberMeView;
//用于修改记住我的图标状态
@property (weak, nonatomic) IBOutlet UIButton *rememberMe;

@property (nonatomic, strong) LoginViewModel *loginVM;

//QQ参数
@property (nonatomic ,strong) NSString *openid;
@property (nonatomic ,strong) NSString *acces_token;
@property (nonatomic ,strong) NSString *expires_str;

@end

@implementation LoginViewController
#pragma mark - view life circle  viewController生命周期方法

- (instancetype)initWithComeFrom:(LoginComeFromType)comeFrom {
    if (self = [super init]) {
        _comeFrom = comeFrom;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self prepareFunc];
    [self prepareUI];
    [self prepareData];

//    self.passwordTextField.text = @"123456";
//    [self observerTextField];
//    [self loginButtonClick:nil];
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.fd_interactivePopDisabled = YES;
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.fd_interactivePopDisabled = NO;
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleDefault;
}


#pragma mark - custom methods  自定义方法
- (void)prepareData{

    if (self.comeFrom == LoginComeFromTypeNormal || self.comeFrom == LoginComeFromTypeUpdatePwd || self.comeFrom == LoginComeFromTypeLogout) {
        
        NSString *account = [[NSUserDefaults standardUserDefaults] objectForKey:@"LoginAccount"];
        NSString *pwd = [[NSUserDefaults standardUserDefaults] objectForKey:@"loginPassword"];
        
        if (account && ![account isEqualToString:@""]) {
            self.accountTextField.text = account;
            
            [self observerTextField];
            
#ifdef DEBUG
            if (self.comeFrom != LoginComeFromTypeLogout && (self.passwordTextField.text.length == 0 || [self.passwordTextField.text isEqualToString:@""]) && pwd.length > 0) {
                [self loginButtonClick:nil];

            }
#endif
        }
        
    }
}


- (void)prepareFunc{
    
    [self isShowBackBtn:NO];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.navigationItem addSimpleTitleView:@"登陆"];
    
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    NSString *app_Name = [infoDictionary objectForKey:@"CFBundleDisplayName"];
    self.appLabel.text = app_Name;
    
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"LoginRememberMeState"];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (void)prepareUI{
    
    [self.avatorImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(126);
        make.height.mas_equalTo(60);
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view).offset(Resize_Height(70));
    }];
    
    [self.appLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.avatorImage.mas_bottom).offset(Resize_Height(15));
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.centerX.equalTo(self.view);
    }];
    
    [self.accountView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(Resize_Width(40));
        make.right.equalTo(self.view).offset(-Resize_Width(40));
        make.height.equalTo(@45);
        make.top.equalTo(self.appLabel.mas_bottom).offset(Resize_Height(55));
    }];
    
    [self.passwordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(Resize_Width(40));
        make.right.equalTo(self.view).offset(-Resize_Width(40));
        make.height.equalTo(@45);
        make.top.equalTo(self.accountView.mas_bottom).offset(Resize_Height(25));
    }];
    
    [self.forgetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.passwordView.mas_right).offset(-10);
//        if (Screen_Width>320) {
//            make.top.equalTo(self.passwordView.mas_bottom).offset(25);
//        }else{
//            make.top.equalTo(self.passwordView.mas_bottom).offset(10);
//        }
        make.top.equalTo(self.passwordView.mas_bottom).offset(10);
        make.width.equalTo(@70);
        make.height.equalTo(@15);
    }];
    
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.accountView);
        make.right.equalTo(self.accountView);
        make.height.equalTo(@45);
        if (Screen_Width>320) {
            make.top.equalTo(self.forgetButton.mas_bottom).offset(Resize_Height(80));
        }else{
            make.top.equalTo(self.forgetButton.mas_bottom).offset(70);
        }
        
    }];
    
    //默认不开启
    self.loginBtn.enabled = NO;
    self.passwordTextField.delegate = self;
    
    [self.accountTextField addTarget:self action:@selector(accountTextFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    
    [self.passwordTextField addTarget:self action:@selector(passwordTextFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    
    if (self.comeFrom == LoginComeFromTypeUpdatePwd) {
//        self.accountTextField.text = [GlobalManage sharedTool].userInfoModel.mobile;
    }
    
}


#pragma mark - 按钮响应
- (void)accountTextFieldEditingChanged:(UITextField *)textField{
    [self observerTextField];
}

- (void)passwordTextFieldEditingChanged:(UITextField *)textField{
    [self observerTextField];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField == self.passwordTextField) {
        [textField resignFirstResponder];
        [self loginButtonClick:nil];
    }
    return YES;
}

- (void)observerTextField{
    if (self.accountTextField.text.length > 0 && self.passwordTextField.text.length > 0) {
        self.loginBtn.enabled = YES;
    }else{
        self.loginBtn.enabled = NO;
    }
}

- (IBAction)forgetButtonClick:(UIButton *)sender {
    [self goToForgetVc];
}

- (IBAction)loginButtonClick:(UIButton *)sender {
    
    NSString *saveAccount = [[NSUserDefaults standardUserDefaults] objectForKey:@"LoginAccount"];
    
    NSString *pwd = [[NSUserDefaults standardUserDefaults] objectForKey:@"loginPassword"];
    
    //网络请求
    NSString *account = self.accountTextField.text;
    NSString *password = [self.passwordTextField.text MD5_lib];
    
    if ((self.passwordTextField.text.length == 0 || [self.passwordTextField.text isEqualToString:@""]) && pwd.length > 0) {
        password = pwd;
    }
    
#warning 测试账号
//    NSString *account = @"13812345678";
//    NSString *password = @"1111111111111111111111111111111";
    if (!account || !password) {
        return;
    }
    
    if (saveAccount&&saveAccount.length>16) {
        account = saveAccount;
        password = [[NSUserDefaults standardUserDefaults] objectForKey:@"loginPassword"];
    }

    if ([self.view.window isKeyWindow]) {
        Show_NoMsg
    }
    
    NSArray *param = @[account,password];
    
    BMWeakSelf
    [self.loginVM requestLoginWithParams:param success:^(id object) {
        [[NSUserDefaults standardUserDefaults] setObject:self.accountTextField.text forKey:@"LoginAccount"];
        [[NSUserDefaults standardUserDefaults] setObject:password forKey:@"loginPassword"];
        if (weakSelf.comeFrom == LoginComeFromTypePresentLogin) {
            if (weakSelf.BlockLoginSuccess) {
                weakSelf.BlockLoginSuccess();
                return;
            }
        }
        //跳转首页
        [[NSNotificationCenter defaultCenter] postNotificationName:Notify_Login_Success object:nil];
    } failure:^(NSString *code, NSString *msg) {
        
    }];
}

- (IBAction)pwdOpenClick:(UIButton *)sender {
    sender.selected = !sender.selected;
    self.passwordTextField.secureTextEntry = !sender.selected;
}
#pragma mark - 跳转
- (void)goToForgetVc{
    ForgetPasswordViewController *regVc = [[ForgetPasswordViewController alloc] init];
    regVc.successRequestNewPwdBlock = ^(NSString *account) {
        self.accountTextField.text = account;
    };
    [self.navigationController pushViewController:regVc animated:YES];
}

#pragma mark - getters
- (LoginViewModel *)loginVM{
    if (!_loginVM) {
        _loginVM = [[LoginViewModel alloc] init];
    }
    return _loginVM;
}

#pragma mark - setters



@end
